package com.bulletelc.apollopre.module.web;

import com.bulletelc.apollopre.MyApplication;
import com.bulletelc.apollopre.constants.GlobalConfig;
import com.bulletelc.apollopre.constants.NetWorkPath;
import com.bulletelc.apollopre.entity.WxShareBean;
import com.bulletelc.apollopre.framework.BasePresenter;
import com.bulletelc.apollopre.utils.MyUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Type;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Created by Administrator on 2018/5/7.
 */

public class WebPagePresenter extends BasePresenter<WebPageContract.View> implements WebPageContract.Presenter {
    @Override
    public void getAgreement(String api) {
        getAgreementImp(api);
    }

    @Override
    public void getBargain(String orderId) {
        getBargainImp(orderId);
    }

    @Override
    public void start() {

    }

    @Override
    public void stop() {

    }


    /**
     * 获取协议
     * @param api 协议api
     */
    private void getAgreementImp(String api) {
        OkGo.<String>get(api)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        parseAgreement(response);
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        if (isViewAttached()) {
                            getView().onErrorAgreement(response.getException().toString());
                        }
                    }
                });
    }

    /**
     * 解析协议内容
     * @param response
     */
    private void parseAgreement(Response<String> response) {
        if (response.isSuccessful()) {
            try {
                JSONObject object = new JSONObject(response.body().toString());
                JSONObject meta = object.getJSONObject("meta");
                if (GlobalConfig.HTTP_RESULT_SUCCESS.equals(meta.getString("code"))) {
                    JSONObject data = object.getJSONObject("data");
                    String content = data.getString("content");
                    if (isViewAttached()) {
                        getView().onSuccessAgreement(content);
                    }
                } else {
                    if (isViewAttached()) {
                        getView().onErrorAgreement(meta.getString("msg"));
                    }
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
            if (isViewAttached()) {
                getView().onFailAgreement(response.body().toString());
            }
        }
    }

    /**
     * 生成砍价
     */
    private void getBargainImp(String orderId) {
        String json = "";
        JSONObject object = new JSONObject();
        try {
            object.put("sales_order_id", orderId);
            object.put("user_id", MyApplication.getUserId());
            json = object.toString();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        final RequestBody requestBody = RequestBody.create(MediaType.parse(GlobalConfig.CONTENT_TYPE_JSON), json);
        OkGo.<String>post(NetWorkPath.GET_ADD_BARGAIN)
                .upRequestBody(requestBody)
                .params(NetWorkPath.HEADER_KEY_AUTH, MyUtil.getAuthToken())
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        parseBargain(response);
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        if (isViewAttached()) {
                            getView().onErrorBargain(response.getException().toString());
                        }
                    }
                });
    }

    /**
     * 解析砍价
     */
    private void parseBargain(Response<String> response) {
        if (response.isSuccessful()) {
            try {
                JSONObject object = new JSONObject(response.body().toString());
                JSONObject meta = object.getJSONObject("meta");
                if (GlobalConfig.HTTP_RESULT_SUCCESS.equals(meta.getString("code"))) {
                    Gson gson = new Gson();
                    Type type = new TypeToken<WxShareBean>() {}.getType();
                    WxShareBean wxShareBean = gson.fromJson(object.getJSONObject("data").toString(), type);
                    if (isViewAttached()) {
                        getView().onSuccessBargain(wxShareBean);
                    }
                } else {
                    if (isViewAttached()) {
                        getView().onErrorBargain(meta.getString("msg"));
                    }
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
            if (isViewAttached()) {
                getView().onFailBargain(response.body().toString());
            }
        }
    }

}
